package com.example.exercise.demo.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

import com.example.exercise.demo.vo.AppReplyVO;
import com.example.exercise.demo.dao.entity.AppReplyEntity;
import com.example.exercise.demo.dao.AppReplyDAO;

/**
* 回复表
*/
@Service
public class AppReplyService {

    @Autowired
    private AppReplyDAO appReplyDAO;

    public List<AppReplyVO> findByWhere(Page<?> page, AppReplyVO appReplyVO){
        List<AppReplyVO> list = new ArrayList<>();
        List<AppReplyEntity> list0 = appReplyDAO.findByWhere(page, appReplyVO);
        for (AppReplyEntity appReplyEntity : list0) {
            list.add(toAppReplyVO(appReplyEntity));
        }
        return list;
    }

    public AppReplyVO findByKey(AppReplyVO appReplyVO){
        AppReplyEntity appReplyEntity = appReplyDAO.findByKey(appReplyVO);
        return toAppReplyVO(appReplyEntity);
    }

    public int insert(AppReplyVO appReplyVO){
        return appReplyDAO.insert(toAppReplyEntity(appReplyVO));
    }

    public int updateByKey(AppReplyVO appReplyVO){
        return appReplyDAO.updateByKey(toAppReplyEntity(appReplyVO));
    }

    public int deleteByKey(AppReplyVO appReplyVO){
        return appReplyDAO.deleteByKey(appReplyVO);
    }

    private AppReplyVO toAppReplyVO(AppReplyEntity appReplyEntity) {
        AppReplyVO appReplyVO = new AppReplyVO();
                appReplyVO.setId(String.valueOf(appReplyEntity.getId()));
        appReplyVO.setUserId(String.valueOf(appReplyEntity.getUserId()));
        appReplyVO.setUserName(appReplyEntity.getUserName());
        appReplyVO.setPapersId(String.valueOf(appReplyEntity.getPapersId()));
        appReplyVO.setDiscussId(String.valueOf(appReplyEntity.getDiscussId()));
        appReplyVO.setContext(appReplyEntity.getContext());
        appReplyVO.setCreatTime(String.valueOf(appReplyEntity.getCreatTime()));

        return appReplyVO;
    }

    private AppReplyEntity toAppReplyEntity(AppReplyVO appReplyVO) {
        AppReplyEntity appReplyEntity = new AppReplyEntity();
                String id = appReplyVO.getId();
        if(id != null) {
            appReplyEntity.setId(Integer.parseInt(appReplyVO.getId()));
        }
        String userId = appReplyVO.getUserId();
        if(userId != null) {
            appReplyEntity.setUserId(Integer.parseInt(appReplyVO.getUserId()));
        }
        appReplyEntity.setUserName(appReplyVO.getUserName());
        String papersId = appReplyVO.getPapersId();
        if(papersId != null) {
            appReplyEntity.setPapersId(Integer.parseInt(appReplyVO.getPapersId()));
        }
        String discussId = appReplyVO.getDiscussId();
        if(discussId != null) {
            appReplyEntity.setDiscussId(Integer.parseInt(appReplyVO.getDiscussId()));
        }
        appReplyEntity.setContext(appReplyVO.getContext());

        return appReplyEntity;
    }

}